package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Override
    @Transactional
    public Boolean submit(Orders orders) {
        //根据订单的收货地址id 获取收货地址
        AddressBook address=addressBookService.getById(orders.getAddressBookId());
        //获取当前下单Id 获取当前用户
        User user=userService.getById(BaseContext.getThreadLocal());
        //设置用户id
        orders.setUserId(BaseContext.getThreadLocal());
        //设置订单用户名
        orders.setUserName(user.getName());
        //设置订单手机号
        orders.setPhone(address.getPhone());
        //设置订单地址
        orders.setAddress(address.getDetail());
        //设置订单号
        orders.setNumber(UUID.randomUUID().toString());
        //设置订单收货人
        orders.setConsignee(address.getConsignee());
        //设置支付方式 1微信2支付宝
        orders.setPayMethod(1);
        //设置订单状态 1代付款 2已付款
        orders.setStatus(2);
        //设置下单时间
        orders.setOrderTime(LocalDateTime.now());
        //设置付款时间
        orders.setCheckoutTime(LocalDateTime.now());
        //获取当前用户的购物车列表
        LambdaQueryWrapper<ShoppingCart> shoppingCartLQW = new LambdaQueryWrapper<>();
        shoppingCartLQW.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        List<ShoppingCart> shoppingCarts=shoppingCartService.list(shoppingCartLQW);
        //清空该用户的购物车信息
        shoppingCartService.remove(shoppingCartLQW);
        //初始化该订单的订单明细表
        List<OrderDetail> orderDetails = new ArrayList<>();
        //初始化订单总金额
        Long orderPrice = 0L;
        //将购物车数据加到订单明细表中
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            //将单条购物车菜品/套餐的内容 属性赋值到订单详细中 排除Id字段
            BeanUtils.copyProperties(shoppingCart,orderDetail,"id");
            //计算订单总金额 累加每笔订单的金额=数量*金额
            long price=orderDetail.getNumber() * orderDetail.getAmount().longValue();
            orderPrice = orderPrice + price;
            orderDetails.add(orderDetail);
        }
        //设置订单总金额
        orders.setAmount(BigDecimal.valueOf(orderPrice));
        //保存订单信息
        this.save(orders);
        //将保存后的订单Id 放入到订单明细表中
        for (OrderDetail orderDetail : orderDetails) {
            orderDetail.setOrderId(orders.getId());
            //将订单明细保存到表中
            orderDetailService.save(orderDetail);
        }

        return true;
    }

    @Override
    @Transactional
    public Boolean again(Orders orders) {
        //根据订单号 查找订单明细集合
        LambdaQueryWrapper<OrderDetail> orderDetailLQW = new LambdaQueryWrapper<>();
        orderDetailLQW.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> orderDetailList=orderDetailService.list(orderDetailLQW);
        //将订单明细中的内容放入到购物车中
        List<ShoppingCart> shoppingCartList = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetailList) {
            ShoppingCart shoppingCart = new ShoppingCart();
            //将订单明细中的数据取出放入到购物车中 排除ID
            BeanUtils.copyProperties(orderDetail,shoppingCart,"id");
            shoppingCart.setUserId(BaseContext.getThreadLocal());
            shoppingCartList.add(shoppingCart);
        }
        shoppingCartService.saveBatch(shoppingCartList);
        return true;
    }
}
